package com.jiyiren.demospring.dao;

import com.jiyiren.demospring.bean.UserInfo;
import com.jiyiren.demospring.config.MConfig;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by yiji on 2017/2/22.
 * 此类独立于各种数据库
 */
@Repository
public class UserInfoDaoImp extends BaseDao implements UserInfoDao {

    private static Log logger= LogFactory.getLog(UserInfoDaoImp.class);

    //JdbcTemplate配置与使用
    //http://1358440610-qq-com.iteye.com/blog/1826816

    /**
     * 通过用户名或者email登录ø
     * @param useroremail
     * @param password
     * @param type
     * @return 返回用户id
     */
    @Override
    public int login(String useroremail,String password,int type) {
        ResultSet resultSet=null;
        try {
            if(type==0) {
                resultSet = BaseSelect("select id from tbuser where username=? and password=?", useroremail, password);
            }else{
                resultSet=BaseSelect("select id from tbuser where email=? and password=?",useroremail,password);
            }
            if(resultSet.next()){
                return resultSet.getInt(1);//登录成功返回userid
            }else{
                return -1;//登录失败
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }


    @Override
    public UserInfo getUserInfo(String useroremail, String password,int type) {
        ResultSet resultSet=null;
        UserInfo userInfo=null;
        try {
            if(type==0) {
                resultSet = BaseSelect("select * FROM tbuser WHERE username=? and password=?", useroremail, password);
            }else{
                resultSet=BaseSelect("select * FROM tbuser WHERE email=? and password=?",useroremail,password);
            }
            if(resultSet.next()){
                userInfo=getUserInfoFromSet(resultSet);
                return userInfo;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public UserInfo getUserInfoById(String userid) {
        ResultSet resultSet=null;
        UserInfo userInfo=null;
        try {
            resultSet=BaseSelect("select * FROM tbuser WHERE id=?",userid);
            if(resultSet.next()){
                userInfo=getUserInfoFromSet(resultSet);
                return userInfo;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }


    private UserInfo getUserInfoFromSet(ResultSet resultSet) throws SQLException {
        UserInfo userInfo=new UserInfo();
        String usernmae=resultSet.getString(MConfig.USERNAME);
//                String password=resultSet.getString(MConfig.PASSWORD);
        String email=resultSet.getString(MConfig.EMAIL);
        String phone=resultSet.getString(MConfig.PHONE);
        String sex=resultSet.getString(MConfig.SEX);
        String motto=resultSet.getString(MConfig.MOTTO);
        String headpicurl=resultSet.getString(MConfig.HEADPICURL);
        String userid=resultSet.getString(MConfig.ID);
        userInfo.setUsername(usernmae);
        userInfo.setEmail(email);
        userInfo.setPhone(phone);
        userInfo.setSex(sex);
        userInfo.setMotto(motto);
        userInfo.setHeadpicurl(headpicurl);
        userInfo.setUserid(userid);
        return userInfo;
    }


}
